import java.awt.List;
import java.util.LinkedList;
import java.util.Queue;


public class Sched {
	roundRobin robin;
	public Sched() {
		robin = new roundRobin();
		
		
		//Testing code
		Process proc1 = new Process("Calculator", 0, 300, "Stuff");
		Process proc2 = new Process("UI", 0, 15, "Stuff");
		Process proc3 = new Process("Busy waiting", 0,200, "Stuff");
		
		addProcess(proc1, 0);
		addProcess(proc2, 0);
		addProcess(proc3, 99);
	}
	 
	
	
	
	
	
	private class roundRobin extends LinkedList {
		private static final int MAX_PROCESSES = 4;
		
	
		public void enqueue(Process process) {
			addLast(process);
		}
	
		public Process dequeue() {
			return (Process) poll();
		}
		
		public boolean hasProcesses() {
			return isEmpty();
		}
		
		public int size() {
			return size();
		}
	}
	public enum caller { TIMER, IO}
	
	protected Process getNextProcess(caller caller ) {
		Process next = null;
		if(caller.equals(caller.TIMER)) {
			next = sysCallTimer();
			System.out.println("Scheduler: Running process " + next.fetchprocId() 
				+ " (" + next.fetchName() + ") next.");
			return next;
		}
		
		return next;
	}
	
	private Process sysCallTimer() {
		Process next = robin.dequeue();
		while(next.fetchState().equals(Process.procState.BLOCKED)) {
			robin.enqueue(next);
			next = robin.dequeue();
		}
		next.setState(Process.procState.RUNNING);
		return next;
	}
	
	protected void addProcess(Process process, int pc) {
		process.setnxtStep(pc);
		process.setState(Process.procState.READY);
		robin.enqueue(process);
	}
	
}
